package com.tour.controller.backend;

import com.tour.common.Const;
import com.tour.common.ResponseCode;
import com.tour.common.ServerResponse;
import com.tour.pojo.EasyUITreeNode;
import com.tour.pojo.User;
import com.tour.service.IScenicService;
import com.tour.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

import javax.servlet.http.HttpSession;

/**
 * Created by geely
 */
@Controller
@RequestMapping("/manage/scenic/")
public class ScenicManageController {


    @Autowired
    private IUserService iUserService;

    @Autowired
    private IScenicService iScenicService;

    @RequestMapping("add_scenic.do")
    @ResponseBody
    public ServerResponse addScenic(HttpSession session,String scenicName,@RequestParam(value = "parentId",defaultValue = "0") int parentId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
        }
        //校验一下是否是管理员
        if(iUserService.checkAdminRole(user).isSuccess()){
            //是管理员
            //增加我们处理地址的逻辑
            return iScenicService.addScenic(scenicName,parentId);

        }else{
            return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
        }
    }

    @RequestMapping("set_scenic_name.do")
    @ResponseBody
    public ServerResponse setScenicName(HttpSession session,Integer scenicId,String scenicName){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //更新scenicName
            return iScenicService.updateScenicName(scenicId,scenicName);
        }else{
            return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
        }
    }

    @RequestMapping("get_children_scenic.do")
    @ResponseBody
    public List<EasyUITreeNode> getChildrenParallelScenic(HttpSession session,@RequestParam(value = "id" ,defaultValue = "0") Integer id){
            //查询子节点的category信息,并且不递归,保持平级
    	System.out.println("======================="+id);
            return iScenicService.getChildrenParallelScenic(id);
    }
    
    @RequestMapping("get_children_address.do")
    @ResponseBody
    public List<EasyUITreeNode> getChildrenParallelAddress(HttpSession session,@RequestParam(value = "id" ,defaultValue = "0") Integer id){
    	//查询子节点的category信息,并且不递归,保持平级
    	System.out.println("======================="+id);
    	return iScenicService.getChildrenParallelAddress(id);
    }

    @RequestMapping("get_deep_scenic.do")
    @ResponseBody
    public ServerResponse getScenicAndDeepChildrenScenic(HttpSession session,@RequestParam(value = "scenicId" ,defaultValue = "0") Integer scenicId){
        User user = (User)session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请登录");
        }
        if(iUserService.checkAdminRole(user).isSuccess()){
            //查询当前节点的id和递归子节点的id
//            0->10000->100000
            return iScenicService.selectScenicAndChildrenById(scenicId);

        }else{
            return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
        }
    }
}
